package com.cy.Service.ServiceImpl;

import Exceptions.SelectException;
import Exceptions.UpdateException;
import Pojo.CheckBox;
import com.cy.Dao.SysRoleDao;
import com.cy.Dao.SysRoleMenuDao;
import com.cy.Service.SysRoleService;
import com.cy.pojo.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public List<SysRole> selectRoles(SysRole role) {
        return sysRoleDao.selectRoles(role);
    }

    @Override
    public int insertRole(SysRole role) {
        /**
         * 参数校验，自己效验
         * 保存校色自身信息
         * */
        int rows=sysRoleDao.insertRole(role);
        /**保存角色和菜单关系数据*/
        sysRoleMenuDao.inesertRoleMenus(role.getId(),
                                        role.getMenuIds());
        return rows;
    }

    @Override
    public SysRole selectById(Integer id) {
        SysRole role=sysRoleDao.selectById(id);
        if(role==null){
            throw new SelectException("记录可能已经不存在");
        }
        return role;
    }

    @Override
    public int updateRole(SysRole role) {
        int row=sysRoleDao.updateRole(role);
        if(row==0){
            throw new UpdateException("更新失败");
        }
        return row;
    }

    @Override
    public List<CheckBox> selectCheckRoles() {
        return sysRoleDao.selectCheckRoles();
    }
}
